package com.ruoyi.project.goods.order.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.project.common.util.ChunQiuTool;
import com.ruoyi.project.goods.order.domain.GoodsOrderHead;
import com.ruoyi.project.goods.order.domain.GoodsOrderModel;
import com.ruoyi.project.goods.order.mapper.GoodsOrderHeadMapper;
import com.ruoyi.project.repertory.domain.GoodsRepertory;
import com.ruoyi.project.repertory.service.IGoodsRepertoryService;
import com.ruoyi.project.system.dict.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.goods.order.mapper.GoodsOrderMapper;
import com.ruoyi.project.goods.order.domain.GoodsOrder;
import com.ruoyi.project.goods.order.service.IGoodsOrderService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 订单Service业务层处理
 * 
 * @author lonsn
 * @date 2020-11-12
 */
@Service
public class GoodsOrderServiceImpl implements IGoodsOrderService 
{
    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Autowired
    private GoodsOrderHeadMapper goodsOrderHeadMapper;

    @Autowired
    private IGoodsRepertoryService goodsRepertoryService;
    /**
     * 查询订单
     *
     * @param orderNo 订单ID
     * @return 订单
     */
    @Override
    public GoodsOrder selectGoodsOrderByOrderNo(String orderNo) {
        return goodsOrderMapper.selectGoodsOrderByOrderNo(orderNo);
    }

    /**
     * 查询订单列表
     * 
     * @param goodsOrder 订单
     * @return 订单
     */
    @Override
    public List<GoodsOrder> selectGoodsOrderList(GoodsOrder goodsOrder)
    {
        return goodsOrderMapper.selectGoodsOrderList(goodsOrder);
    }

    /**
     * 新增订单
     * 
     * @param orderModel 订单
     * @return 结果
     */
    @Override
    public int insertGoodsOrder(GoodsOrderModel orderModel)
    {
        //计算订单总金额
        Double totalMoney= 0.0;//订单总金额
        String orderNo=ChunQiuTool.createOrderNo();//订单号
        orderModel.setOrderNo(orderNo);
        List<GoodsOrderHead> list= orderModel.getGoods();
        //循环订单明细，1：计算订单总金额； 2：合并同类项
        for (GoodsOrderHead head : list){
            totalMoney=totalMoney+head.getMoney();
            head.setOrderNo(orderNo);
            Double price=head.getMoney()/head.getQuantity();
            head.setPreferentialPrice(price);
        }

        orderModel.setOrderMoney(totalMoney);
        //1. 生成订单数据
        GoodsOrder goodsOrder=new GoodsOrder();
        goodsOrder.setCreateBy(orderModel.getCreateBy());
        goodsOrder.setOrderNo(orderModel.getOrderNo());
        goodsOrder.setOrderDate(new Date());
        goodsOrder.setOrderTime(new Date());
        goodsOrder.setOrderMoney(orderModel.getOrderMoney());
        goodsOrder.setDeptId(orderModel.getDeptId());
        goodsOrder.setStatus(1);//订单状态，默认未支付

        int x=goodsOrderMapper.insertGoodsOrder(goodsOrder);
        //2. 生成订单明细
        int y=goodsOrderHeadMapper.insertBatchGoodsOrderHead(list);
        return y;
    }




    /**
     * 修改订单
     * 
     * @param goodsOrder 订单
     * @return 结果
     */
    @Override
    public int updateGoodsOrder(GoodsOrder goodsOrder)
    {
        return goodsOrderMapper.updateGoodsOrder(goodsOrder);
    }

    /**
     * 删除订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteGoodsOrderByIds(String ids)
    {
        return goodsOrderMapper.deleteGoodsOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单信息
     * 
     * @param orderNo 订单ID
     * @return 结果
     */
    @Override
    public int deleteGoodsOrderById(String orderNo)
    {
        return goodsOrderMapper.deleteGoodsOrderById(orderNo);
    }

    @Override
    public int paySuccess(GoodsOrder goodsOrder) {
        //1.更新订单状态，支付渠道
        goodsOrder.setStatus(Integer.parseInt(DictUtils.getDictValue("order_status","支付成功")));
        goodsOrderMapper.updateGoodsOrder(goodsOrder);
        //刷新订单信息
        goodsOrder=goodsOrderMapper.selectGoodsOrderByOrderNo(goodsOrder.getOrderNo());
        //获取订单明细
        GoodsOrderHead h=new GoodsOrderHead();
        h.setOrderNo(goodsOrder.getOrderNo());
        List<GoodsOrderHead> list=goodsOrderHeadMapper.selectGoodsOrderHeadList(h);
        //2. 减库存
        Integer deptId=goodsOrder.getDeptId();
        int rows=0;
        for (GoodsOrderHead head : list){
            String goodsName=head.getGoodsName();
            //查最新库存
            GoodsRepertory repertory=goodsRepertoryService.selectGoodsRepertory(deptId,head.getStyleNumber());
            //3. 判断库存

            //实时处理库存，先屏蔽

//            if(repertory==null){
//                throw new BusinessException(goodsName+"无库存");
//            }
//            Integer repertoryQuantity=repertory.getQuantity();
//            if(repertoryQuantity-head.getQuantity()<0){
//                throw new BusinessException(goodsName+"库存不够");
//            }
            if(repertory!=null){
                GoodsRepertory goodsRepertory=new GoodsRepertory();
                goodsRepertory.setStyleNumber(head.getStyleNumber());
                goodsRepertory.setDeptId(goodsOrder.getDeptId());

                goodsRepertory.setQuantity(repertory.getQuantity()-head.getQuantity());
                goodsRepertoryService.updateGoodsRepertory(goodsRepertory);
                rows++;
            }

        }
        return rows;
    }
}
